const common_user = require('../models/common_user')
const jwt  = require('jsonwebtoken')
const bctypt = require('bcryptjs')

// 登陆
const login = async (ctx) => {
  const data = ctx.request.body
  const userInfo = await common_user.login(data)
  if(userInfo!=null) {
    if(data.password!=userInfo.password) {
      ctx.body = {
        success: false,
        message: '密码错误'
      }
    }else if(userInfo.status !=1){
      ctx.body = {
        success: false,
        message: '该用户已被禁用'
      }
    }else {
      const userToken = {
        name: userInfo.name,
        id: userInfo.id
      }
      const secret = 'vue-campus'
      const token = jwt.sign(userToken, secret)
      ctx.body = {
        success: true,
        data: {
          token: token
        }
      }
    }
  }else {
    ctx.body = {
      success: false,
      message: '用户不存在'
    }
  }
}
// 注册
const register = async (ctx) => {
  const data = ctx.request.body
  const result = await common_user.login(data)
  console.log(result)
  if(result!=null) {
    if(result.name) {
      ctx.body = {
        code: -1,
        message: '账号已被注册'
      }
    }
  }else {
    const result = common_user.register(data)
    if(result) {
      ctx.body = {
        code: 0,
        message: '注册成功'
      }
    }else {
      ctx.body = {
        code: -1,
        message: '注册失败'
      }
    }
  }
}
// 获取用户信息
const getInfo = async (data) => {
  const userInfo = await common_user.getInfo(data)
  return userInfo
}
// 修改用户信息
const updateInfo = async (name, ctx) => {
  const value = ctx.request.body
  const column = ctx.params.column
  const result = await common_user.updateInfo(name, value.column, column)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}

// 获取全部用户
const getCommonUsers = async (ctx) => {
  const query = ctx.request.query
  const data = await common_user.getCommonUsers(query)
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
}
// 根据id获取用户
const getUserById = async (ctx) => {
  const id = ctx.params.id
  const data = await common_user.getUserById(id)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败',
    }
  }
}
// 根据id修改用户状态
const updateStatus = async (ctx) => {
  const id = ctx.params.id
  const data = ctx.request.body
  const result = await common_user.updateStatus(id, data)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 修改个人余额
const updateBalance = async (ctx, name) =>{
  const data = ctx.request.body
  const result = await common_user.updateBalance(data, name)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 修改密码
const modifyPassword = async (ctx, name) => {
  const data = ctx.request.body
  const result = await common_user.modifyPassword(data, name)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 设置支付密码
const modifyPayPassword = async (ctx, name) => {
  const data = ctx.request.body
  const result = await common_user.modifyPayPassword(data, name)
  if(result) {
    ctx.body = {
      code: 0,
      message: '成功'
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
// 根据关键字获取用户
const getUserByKeyWords = async (ctx) => {
  const keywords = ctx.request.query
  const data = await common_user.getUserByKeyWords(keywords)
  if(data) {
    ctx.body = {
      code: 0,
      message: '成功',
      data
    }
  }else {
    ctx.body = {
      code: -1,
      message: '失败'
    }
  }
}
module.exports = {
  getCommonUsers,
  updateStatus,
  login,
  register,
  getInfo,
  updateInfo,
  getUserById,
  updateBalance,
  modifyPassword,
  modifyPayPassword,
  getUserByKeyWords
}